How to get browser timezone in moment.js?

Member

by scotty , in category: JavaScript , 2 years ago

How to get browser timezone in moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by dmitrypro77 , 2 years ago

@scotty You can use moment-timezone package to work with timezone in moment.js and get browser timezone using tz.guess() and below is code as example:


1
2
3
4
5
6
import moment from "moment";
import 'moment-timezone';

const timezone = moment.tz.guess();

console.log(timezone);

Member

by ewald , a year ago

@scotty 

You can get the browser timezone using moment.js by simply using the 'moment.tz.guess()' method. This will return the timezone identifier based on the browser's settings.


Here's an example code snippet:

1
2
var browserTimezone = moment.tz.guess();
console.log(browserTimezone);


This will output the browser timezone identifier in the console. You can then use this timezone identifier with moment.js to display, convert, or manipulate dates and times.